/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.installer;

import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import sshtunneler.installer.launchergenerator.BashLauncherGenerator;
import sshtunneler.installer.launchergenerator.CmdLauncherGenerator;
import sshtunneler.installer.launchergenerator.LauncherGenerator;

public class Config {

	public static Config inst = new Config();

	// Config
	public String desktop_path;
	public String userhome_path;
	public boolean is_windows = false;
	public boolean is_vista = false;
	public boolean is_linux = false;
	public boolean is_mac = false;

	// Some system properties, used later
	private String sys = System.getProperty("os.name");
	@SuppressWarnings("unused")
	private String arc = System.getProperty("os.arch");
	@SuppressWarnings("unused")
	private String osv = System.getProperty("os.version");
	private String lang = Locale.getDefault().getLanguage();

	// Some stuff for external config
	private ResourceBundle config_resourceMap;
	private ResourceBundle script_resourceMap;
	private ResourceBundle license_resourceMap;

	// External config
	public boolean ask_username;
	public boolean overwrite_all;
	public boolean create_debug_link;
	public boolean start_minimized;
	public boolean connect_on_startup;
	public boolean hide_tool_config;
	public String ports_config;

	// User answers
	public String username = "";

	protected Config() {
		this.init();
	}

	private void infer_userhome_path() {
		this.userhome_path = System.getProperty("user.home");
		if (this.is_windows && !this.is_vista && this.lang.equals("fr")) {
			this.desktop_path = this.userhome_path + File.separator + "bureau";
		} else if (this.is_windows) { // Windows ang or Vista
			// Vista, fr or an is 'desktop'
			this.desktop_path = this.userhome_path + File.separator + "desktop";
		} else { // Linux or mac ?
			this.desktop_path = this.userhome_path + File.separator + "Desktop";
		}
	}

	public void init() {
		this.config_resourceMap = ResourceBundle
				.getBundle("sshtunneler.installer.Config");
		this.script_resourceMap = ResourceBundle
				.getBundle("sshtunneler.installer.Script");
		this.license_resourceMap = ResourceBundle
				.getBundle("sshtunneler.installer.license");
		if (this.sys.startsWith("Windows")) {
			this.is_windows = true;
			if (this.sys.equals("Windows Vista")) {
				this.is_vista = true;
			}
		}
		if (this.sys.startsWith("Linux")) {
			this.is_linux = true;
		}
		if (this.sys.startsWith("Mac OS")) {
			this.is_mac = true;
		}

		this.infer_userhome_path();

		this.ask_username = Boolean.parseBoolean(this.config_resourceMap
				.getString("config.ask_username"));
		this.overwrite_all = Boolean.parseBoolean(this.config_resourceMap
				.getString("config.overwrite_all"));
		this.connect_on_startup = Boolean.parseBoolean(this.config_resourceMap
				.getString("config.connect_on_startup"));
		this.start_minimized = Boolean.parseBoolean(this.config_resourceMap
				.getString("config.start_minimized"));
		this.hide_tool_config = Boolean.parseBoolean(this.config_resourceMap
				.getString("config.hide_tool_config"));
		this.create_debug_link = Boolean.parseBoolean(this.config_resourceMap
				.getString("link.debug_link"));
		this.ports_config = this.config_resourceMap.getString("config.ports");
	}

	public File getLauncherFile() {
		return new File(Config.inst.desktop_path + "/SSHTunneler.cmd");
	}

	public File getConfigFile() {
		return new File(Config.inst.userhome_path + File.separator
				+ "SSHTunneler_Default.ssc");
	}

	public String getScript() {
		return this.script_resourceMap.getString("script");
	}

	public LauncherGenerator getLauncherGenerator() {
		if (this.is_windows) {
			return new CmdLauncherGenerator();
		} else if (this.is_linux || this.is_mac) {
			return new BashLauncherGenerator();
		} else {
			int ret = JOptionPane.showConfirmDialog(null,
					"Are you using Microsoft Windows ? ",
					"Could not determine OS", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if (ret == JOptionPane.YES_OPTION) {
				this.is_windows = true;
			} else {
				this.is_linux = true;
			}

			this.infer_userhome_path();

			return this.getLauncherGenerator();
		}
	}

	public String getDefaultConfig() {
		StringBuilder ret = new StringBuilder();

		ret.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		ret.append("<SSHTunnelerConfig>\n");
		ret.append("    <DefaultUsername>" + this.username
				+ "</DefaultUsername>\n");
		ret.append("    <AutoSaveOptions>false</AutoSaveOptions>\n");
		ret
				.append("    <AutoSaveServersAndTunnels>false</AutoSaveServersAndTunnels>\n");
		ret.append("    <StartMinimized>" + this.start_minimized
				+ "</StartMinimized>\n");
		ret.append("    <ConnectToAllOnStart>" + this.connect_on_startup
				+ "</ConnectToAllOnStart>\n");
		ret.append("    <HideToolsConfig>" + this.hide_tool_config
				+ "</HideToolsConfig>\n");
		ret.append("" + this.ports_config + "\n");
		ret.append("</SSHTunnelerConfig>\n");

		return ret.toString();
	}
	
	public String getLicense()
	{
		return this.license_resourceMap.getString("license");
	}
}
